package com.helper.dream.a361phonehelper.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.helper.dream.a361phonehelper.R;
import com.helper.dream.a361phonehelper.bean.TuijianYuleEntity;
import com.helper.dream.a361phonehelper.inter.ITuijianYuleiInterface;
import com.helper.dream.a361phonehelper.utils.ApiManager;
import com.helper.dream.a361phonehelper.utils.L;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by LamYu on 16/12/19/0019.
 */

public class TuijianYuleFragment extends Fragment {
    View view;
    LayoutInflater inflater;
    LinearLayout ll;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view=inflater.inflate(R.layout.fragment_tuijianyule,container,false);
        this.inflater=inflater;
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ll= (LinearLayout) view.findViewById(R.id.ll_tj_yl);
        getData();
    }
    List<TuijianYuleEntity.TagsBean> tagsBeen;
    private void getData() {
        Retrofit retrofit=new Retrofit.Builder().addConverterFactory(GsonConverterFactory.create()).baseUrl(ApiManager.TjYlBase).build();
        ITuijianYuleiInterface iTuijianYuleiInterface=retrofit.create(ITuijianYuleiInterface.class);
        Call<TuijianYuleEntity> call=iTuijianYuleiInterface.getTjYule();
        call.enqueue(new Callback<TuijianYuleEntity>() {
            @Override
            public void onResponse(Call<TuijianYuleEntity> call, Response<TuijianYuleEntity> response) {
                tagsBeen=response.body().getTags();
                View tag = inflater.inflate(R.layout.item_tj_yl_tag, null);
                LinearLayout lltag= (LinearLayout) tag.findViewById(R.id.ll_tj_yl_tag);
                for (int i = 0; i < tagsBeen.size(); i++) {
                    L.d(tagsBeen.get(i).getTitle());
                    View tagItem = inflater.inflate(R.layout.item_tj_yl_tag_item, null);
                    ImageView iv= (ImageView) tagItem.findViewById(R.id.iv_tj_yl_tag);
                    Glide.with(getActivity()).load(tagsBeen.get(i).getLogo()).into(iv);
                    TextView tv= (TextView) tagItem.findViewById(R.id.tv_tj_yl_tag);
                    tv.setText(tagsBeen.get(i).getTitle());
                    lltag.addView(tagItem);
                    ll.addView(tag);
                }
            }

            @Override
            public void onFailure(Call<TuijianYuleEntity> call, Throwable t) {
                L.d("加载失败");
            }
        });
    }
}
